<template>
  <!-- 修改资料 -->

  <div>
    <el-form label-width="85px" :model="form" label-position="right" ref="formRef" :rules="formRules">
      <el-form-item label="编号" prop="number">
        <el-input v-model="form.number"></el-input>
      </el-form-item>
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="上级职位" prop="parentId">
        <el-tree-select v-model="form.parentId" :data="parentData" :render-after-expand="false" check-strictly="true" />
      </el-form-item>
      <el-form-item label="级别" prop="itemIndex">
        <el-input-number v-model="form.itemIndex" :min="0" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch v-model="form.status" size="large" inline-prompt :active-icon="Check" :inactive-icon="Close"
          active-value="normal" inactive-value="disable" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <div class="button-position">
      <el-button @click="updateVisible" style="margin-left: 228px">取 消</el-button>
      <el-button type="primary" @click="addPosition(formRef)">确 定</el-button>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, getCurrentInstance, watch, onMounted } from "vue";
import { ElNotification } from "element-plus";
import { Check, Close } from '@element-plus/icons-vue';
import Api from '@/api/model/user'
const visible = ref(true)
const emit = defineEmits(['updatetable'])
onMounted(() => {
  getParent();
});

const props = defineProps({
  paramsData: {
    type: Object,
    defalut: {},
  },
});

// 添加用户数据的对象
const form = reactive({
  number: "",
  name: "",
  parentId: "",
  status: "normal",
  remark: "",
  itemIndex: 1,
});

const formRef = ref();
const formRules = reactive({
  number: [{ required: true, message: "请输入编号", trigger: "blur" }],
  name: [{ required: true, message: "请输入名称", trigger: "blur" }],
});

const updateVisible = (val) => {
  emit('updatetable')
};

const parentData = ref([]);
const getParent = async () => {
  let params = {
    fileName: "PositionTreeReport",
  };
  let res = await Api.position.positionTree.post(params);
  parentData.value = res.reportData;
};

const restAddFormData = () => {
  formRef.value.resetFields();
};

// 上传
const addPosition = async (formRef) => {
  await formRef.validate((valid, fields) => {
    if (valid) {
      addPositionData();
    }
  });
};

const addPositionData = async () => {
  // console.log(form.name);
  var params = {
    bean: form,
  };
  var res = await Api.position.save.post(params);
  if (res.code == 200) {
    restAddFormData();
    ElNotification.success({
      title: "",
      message: "添加成功！",
      duration: 2000,
    });
    emit("updatetable", true);
  }
};
</script>

<style scoped></style>

